const app = getApp();
import services from '../../services/clockin';
import moment from 'moment';
Page({
  data: {
    isReady: false,
    isLoading: false,
    unionid: '',
    title: '',
    id: '', // 员工id
    name: '',
    statistic_day: '', // 日期
    tableData: [],  // 日报表数据
    tableDataCardDetail: [] // 日报表详情数据-打卡记录列表
  },
  async onLoad(query) {
    const {
      unionid
    } = app.globalData.userInfo || {};
    const {
      id,
      name,
      statistic_day
    } = query || {}
    this.setData({
      id,
      name,
      statistic_day,
      title: `${decodeURIComponent(name)}${moment(statistic_day).format('YYYY-MM-DD')}打卡详情表`,
      unionid: unionid || '2QV8HeciS9zgYhRAtxbpiPZwiEiE'
    });
    await this.initData();
    this.setData({
      isReady: true
    });
  },
  async onShow() {
    if (this.data.isReady) {
      await this.initData();
    }
  },
  async initData() {
    await this.queryGetReportEmployeeDaily();
    await this.queryEmployeeDailyCardDetail();
  },
  async queryGetReportEmployeeDaily() {
    const {
      id,
      statistic_day
    } = this.data;
    console.log('queryGetReportStationDaily', statistic_day);
    if (!statistic_day || !id) {
      dd.showToast({
        type: 'info',
        content: '暂无员工信息',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        statistic_day: statistic_day // 日期
      };
      const res = await services.getReportEmployeeDailyReport(id, params, this.data.unionid);
      console.log('queryGetReportStationDaily', params, res);
      if (res && res.data) {
        this.setData({
          tableData: [...res.data]
        });
        console.log('tableData', this.tableData);
      }
    } catch (error) {
      console.log('error', error);
      this.setData({
        tableData: []
      });
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  async queryEmployeeDailyCardDetail() {
    // 查询员工日度打卡详情
    const {
      id,
      statistic_day
    } = this.data;
    console.log('queryEmployeeDailyCardDetail', statistic_day, id);
    if (!statistic_day || !id) {
      dd.showToast({
        type: 'info',
        content: '暂无员工信息',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      console.log('exit queryEmployeeDailyCardDetail', 'loading');
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const {
        unionid
      } = app.globalData.userInfo || {};
      const params = {
        user_id: id,
        statistic_day: statistic_day // 日期
      };
      const res = await services.getReportEmployeeDailyCardDetail(unionid, params);
      console.log('query user daily card detail', params, res);
      if (res && res.data) {
        this.setData({
          tableDataCardDetail: [...res.data.record_list]
        });
        console.log('tableDataCardDetail', this.tableDataCardDetail);
      }
    } catch (error) {
      console.log('error', error);
      this.setData({
        tableDataCardDetail: []
      });
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  async deleteCardRecord(e) {
    const {
      id
    } = e.currentTarget.dataset;
    console.log('deleteRow', id);
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true,
    });
    if (id>0) {
      try {
        const params = {
          clock_id: id
        };
        const res = await services.deleteEmployeeDailyCardRecord(params, this.data.unionid);
        console.log('deleteRow', params, res);
        if (res && res.data) {
          dd.showToast({
            type: 'success',
            content: '删除成功',
            duration: 1000
          });
          this.setData({
            isLoading: false,
            tableDataCardDetail: [],
            tableData: []
          });
          // 重新加载页面数据
          await this.initData();
        }
      } catch (error) {
        console.log('error', error);
        dd.showToast({
          type: 'info',
          content: `删除失败, ${error.data}`,
          duration: 1000
        });
      } finally {
        this.setData({
          isLoading: false
        });
      }
    }
  }
});